﻿using Core;
using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Services.Localization
{
    /// <summary>
    /// 本地化扩展
    /// </summary>
    public static class LocalizationExtensions
    {
        /// <summary>
        /// 获取本地化枚举标识
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="enumValue">枚举值</param>
        /// <param name="localizationService">本地化服务</param>
        /// <returns></returns>
        public static string GetLocalizedEnum<T>(this T enumValue, ILocalizationService localizationService)
        where T : struct
        {
            if (localizationService == null)
                throw new ArgumentNullException("localizationService");

            if (!typeof(T).IsEnum) throw new ArgumentException("T必须是枚举类型");

            //本地化资源值
            string resourceName = string.Format("Enums.{0}.{1}",
                typeof(T).ToString(),
                //Convert.ToInt32(enumValue)
                enumValue.ToString());
            string result = localizationService.GetResource(resourceName, false, "", true);

            //设置默认值
            if (String.IsNullOrEmpty(result))
                result = resourceName;
                //result = CommonHelper.ConvertEnum(enumValue.ToString());

            return result;
        }

        /// <summary>
        /// 枚举类型获取字符标识
        /// </summary>
        /// <typeparam name="T">实体对象</typeparam>
        /// <param name="enumValue">枚举值</param>
        /// <param name="localizationService">本地化服务</param>
        /// <param name="workContext">当前上下文</param>
        /// <returns></returns>
        public static string GetLocalizedEnum<T>(this T enumValue, ILocalizationService localizationService, IWorkContext workContext)
          where T : struct
        {
            if (workContext == null)
                throw new ArgumentNullException("workContext");

            return GetLocalizedEnum<T>(enumValue, localizationService);
        }
    }
}
